package it.lefo.dateReminder;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import it.lefo.dateReminder.bo.ContactsRepository;
import it.lefo.dateReminder.helper.DbHelper;
import it.lefo.dateReminder.helper.Utilities;
import it.lefo.dateReminder.model.Contact;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class SmsView extends Activity implements OnClickListener {
	
	private EditText txtPhoneNo;
	private EditText txtSms;
	private Button btnSendSms;
	private Button btnSendAndSaveSms;
	private boolean isValidForm = false;	
	private ContactsRepository _cntRepo;
	private SQLiteDatabase _db;
	private Contact _c;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.addevent);
	    
	    //DB connection
        DbHelper dbh = new DbHelper(this);          
		try {
			//db connection
			_db = dbh.getOrCreateDataBase();
			_cntRepo = new ContactsRepository(_db);
		}catch(Exception e){
			Log.e("AddEventView", "Errore durante la connessione al db. Exc Message: " + e.getMessage() + ". Exc StakTrace: " +  e.getStackTrace());
		}

	    //FindElements
		txtPhoneNo = (EditText)findViewById(R.id.txtPhoneNo);
		txtSms = (EditText)findViewById(R.id.txtMessage);
		btnSendSms = (Button)findViewById(R.id.btnSendSms);
		btnSendAndSaveSms = (Button)findViewById(R.id.btnSendAndSaveSms);
	    
	    //Get all the get parameters
	    Bundle bundle = this.getIntent().getExtras();	    
	    _c = new Contact();
	    if (bundle != null)
	    	_c = (Contact)bundle.getSerializable("contact");
	    
	    txtPhoneNo.setText(_c.getPhone());
	    txtSms.setText(_c.getTxtmessage());
	    btnSendSms.setOnClickListener(this);
	    btnSendAndSaveSms.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		ValidateForm();
		if (v == btnSendSms){
			if (isValidForm){
				sendSMS(txtPhoneNo.getText().toString(), txtSms.getText().toString());  
			}else
			{
				Toast toast = Toast.makeText(this, "Dati non validi. Correggere e riprovare", Toast.LENGTH_LONG);
				toast.show();
			}
		}else if(v == btnSendAndSaveSms){
			ContentValues cv = new ContentValues();
			cv.put("txtmessage", txtSms.getText().toString());
			_db.update("contacts", cv, "txtmessage=?", new String[]{txtSms.getText().toString()});
			
			if (isValidForm){
				sendSMS(txtPhoneNo.getText().toString(), txtSms.getText().toString());  
			}else
			{
				Toast toast = Toast.makeText(this, "Dati non validi. Correggere e riprovare", Toast.LENGTH_LONG);
				toast.show();
			}
		}
	}
	
	private void sendSMS(String phoneNo, String sms) {
//		PendingIntent pi = PendingIntent.getActivity(this, 0,
//	            new Intent(this, SMS.class), 0);                
        SmsManager smsMng = SmsManager.getDefault();
        smsMng.sendTextMessage(phoneNo, null, sms, null, null);        
        
        Toast toast = Toast.makeText(this, "Messaggio inviato", Toast.LENGTH_LONG);
		toast.show();
	}

	private void ValidateForm(){
		String txtPhoneNo_value = txtPhoneNo.getText().toString();
		String txtSms_value = txtSms.getText().toString();
		
		if (((txtPhoneNo_value.length() == 0) || (txtSms_value.length() == 0) || (!Utilities.isAValidPhoneNumber(txtPhoneNo.getText().toString()))))
			isValidForm = false;
		else
			isValidForm = true;
	}

}
